package com.zufangbao.earth.web.controller.finance;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.Result;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.zufangbao.sun.entity.factoring.FactoringContract;
import com.zufangbao.sun.entity.finance.Charge;
import com.zufangbao.sun.service.ChargeService;
import com.zufangbao.sun.service.DeductService;
import com.zufangbao.sun.service.FactoringContractService;
import com.zufangbao.sun.service.PayService;

@Controller
@RequestMapping("/riskbail")
@MenuSetting("menu-finance")
public class RiskBailController {

  @Autowired
  private FactoringContractService factoringContractService;
  @Autowired
  private DeductService deductService;
  @Autowired
  private PayService payService;
  @Autowired
  private ChargeService chargeService;

  @RequestMapping(value = { "", "/list" })
  @MenuSetting("submenu-riskbail-list")
  public ModelAndView riskBailList(Page page) {

    ModelAndView modelAndView = new ModelAndView(
        "finance/riskbail/riskbail-list");

    Order orderByModifyTime = new Order();
    orderByModifyTime.add("modify_time", "DESC");

    List<Charge> chargelist = chargeService.list(Charge.class,
        orderByModifyTime, page);

    modelAndView.addObject("chargelist", chargelist);
    return modelAndView;

  }

  @RequestMapping(value = "/add")
  @MenuSetting("submenu-riskbail-list")
  public ModelAndView addRiskBail() {

    ModelAndView modelAndView = new ModelAndView(
        "finance/riskbail/riskbail-add");

    List<FactoringContract> factoringContractList = factoringContractService
        .loadAll(FactoringContract.class);

    modelAndView.addObject("factoringContractList", factoringContractList);
    return modelAndView;

  }

  @RequestMapping("/{chargeId}/add")
  @MenuSetting("submenu-riskbail-list")
  public ModelAndView addRiskBail(@PathVariable("chargeId") Long chargeId) {

    ModelAndView modelAndView = new ModelAndView(
        "finance/riskbail/riskbail-add");

    Charge charge = chargeService.load(Charge.class, chargeId);
    List<FactoringContract> factoringContractList = factoringContractService
        .loadAll(FactoringContract.class);

    modelAndView.addObject("factoringContractList", factoringContractList);
    modelAndView.addObject("charge", charge);
    return modelAndView;

  }

  @RequestMapping(value = "/dodeduct", method = RequestMethod.POST)
  @MenuSetting("submenu-riskbail-list")
  public @ResponseBody String doDeduct(@RequestParam("chargeId") Long chargeId,
      @RequestParam("factoringContractId") String factoringContractId,
      @RequestParam("amount") String amount,
      @RequestParam("batchNo") String batchNo,
      @RequestParam("remark") String remark) {

    Result result = new Result();

    result = deductService.deductRiskBail(chargeId,
        Long.parseLong(factoringContractId), batchNo, new BigDecimal(amount),
        remark);

    return JsonUtils.toJsonString(result);

  }

  @RequestMapping(value = "/dopay", method = RequestMethod.POST)
  @MenuSetting("submenu-riskbail-list")
  public @ResponseBody String doPay(@RequestParam("chargeId") Long chargeId) {

    Result result = new Result();

    result = payService.payRiskBail(chargeId);

    return JsonUtils.toJsonString(result);

  }
}
